package com.hifar.test.structure.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hifar.common.QueryParamBean;
import com.hifar.fw.utils.bean.BusiConst;
import com.hifar.log.AutoLogUtils;
import com.hifar.model.VO.QueryParamsVO;
import com.hifar.plat.officUtil.ExcelTemplateUtil;
import com.hifar.test.structure.pojo.HfEnvResPersonTrainPlan;
import com.hifar.test.structure.service.IHfEnvResPersonTrainPlanService;
import com.hifar.sys.BusiResponse;
import com.hifar.sys.PageResult;
import com.hifar.sys.RequestConver;
import com.hifar.sys.SystemUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * @author zhuWeiWei
 * @description 体系管理-培训计划
 * @date 2025/2/14 11:24
 */
@RestController
@RequestMapping("/api/busi/test/envResPersonTrainPlan")
@Api(tags = "体系管理-培训计划")
public class HfEnvResPersonTrainPlanController {
    @Autowired
    private IHfEnvResPersonTrainPlanService envResPersonTrainPlanService;

    @Autowired
    private AutoLogUtils autoLogUtils;

    @ApiOperation("新增")
    @PostMapping("/add")
    public BusiResponse add(@RequestBody @Validated HfEnvResPersonTrainPlan envResPersonTrainPlan) {
        envResPersonTrainPlanService.save(envResPersonTrainPlan);
        autoLogUtils.addLog("环境试验-培训计划", String.format("新增培训计划:【%s】", envResPersonTrainPlan.getTrainTheme()));
        return BusiResponse.OK();
    }


    @ApiOperation("编辑")
    @PostMapping("/edit")
    public BusiResponse edit(@RequestBody @Validated HfEnvResPersonTrainPlan envResPersonTrainPlan) {
        envResPersonTrainPlanService.updateById(envResPersonTrainPlan);

        autoLogUtils.modifyLog("环境试验-培训计划", String.format("编辑培训计划:【%s】", envResPersonTrainPlan.getTrainTheme()));
        return BusiResponse.OK();
    }

    @ApiOperation("删除")
    @PostMapping("/delete")
    public BusiResponse delete(@RequestBody List<String> idList) {
        envResPersonTrainPlanService.removeByIds(idList);

        autoLogUtils.modifyLog("环境试验-培训计划", String.format("删除%d条培训计划", idList.size()));
        return BusiResponse.OK();
    }


    @ApiOperation(("分页查询"))
    @PostMapping("/listPage")
    public BusiResponse listPage(@RequestBody QueryParamsVO queryParamsVO) {
        QueryParamBean queryParamBean = new QueryParamBean<>(queryParamsVO, HfEnvResPersonTrainPlan.class);
        QueryWrapper queryWrapper = queryParamBean.getQueryWrapper();
        Page page = queryParamBean.getPage();
        Page pageData = envResPersonTrainPlanService.page(page, queryWrapper);
        PageResult pageResult = SystemUtil.parsePage(page, pageData);
        return BusiResponse.OK(pageResult);
    }


    @ApiOperation("查询详情")
    @GetMapping("/queryById")
    public BusiResponse queryById(@RequestParam("id") String id) {
        HfEnvResPersonTrainPlan hfEnvResPersonTrainPlan = envResPersonTrainPlanService.getById(id);
        return BusiResponse.OK(hfEnvResPersonTrainPlan);
    }


    @PostMapping("/exportExcel")
    @ApiOperation(value = "培训计划功能:导出数据")
    public Object exportExcel(HttpServletRequest request) {
        Map<String, Object> pageInfo = RequestConver.getPageInfo(request);
        if (pageInfo == null) {
            return BusiResponse.error("参数有误！");
        }
        BusiResponse export = envResPersonTrainPlanService.exportExcel(pageInfo);
        if (export.getCode() == BusiConst.CODE_SUCCESS) {
            String encode = null;
            try {
                encode = URLEncoder.encode("培训计划.xlsx", "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            HttpHeaders headers = new HttpHeaders();
            headers.add("Content-Type", "application/vnd.ms-excel");
            headers.add("Access-Control-Expose-Headers", "Content-Disposition");
            headers.add("Content-Disposition", "filename=" + encode);

            autoLogUtils.exportLog("环境试验-培训计划", "导出培训计划列表");

            return ResponseEntity.status(HttpStatus.OK).headers(headers).body((byte[]) export.getData());
        }
        return export;
    }

    @PostMapping("/importExcel")
    @ApiOperation(value = "培训计划功能:导入excel数据")
    public BusiResponse importExcel(@RequestParam("file") MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            return BusiResponse.error("文件为空！");
        } else {
            if (ExcelTemplateUtil.isExcelFile(file)) {
                autoLogUtils.importLog("环境试验-培训计划", "导入培训计划列表");
                return envResPersonTrainPlanService.importExcel(file);
            } else {
                return BusiResponse.error("请上传excel文件！");
            }
        }
    }



}
